package com.mingmen.supplier.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.supplier.R;
import com.mingmen.supplier.defineview.GlideRoundTransform;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.model.DingListModel;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class YiWanChengListAdapter extends RecyclerView.Adapter<YiWanChengListAdapter.ViewHolder> {
    List<DingListModel.DataBean> myLiveList = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;

    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<DingListModel.DataBean> myLiveList);
    }


    public YiWanChengListAdapter(Context context, List<DingListModel.DataBean> myLiveList) {
        this.mContext = context;
        this.myLiveList = myLiveList;

    }

    public void setDataList(List<DingListModel.DataBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wancheng_already, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv_label.setText(myLiveList.get(position).getRestaurantName());
        DingListModel.DataBean dataBean = myLiveList.get(position);
        if (position == checkedPosition) {
//            holder.content.setBackgroundColor(Color.parseColor("#f3f3f3"));
//            holder.content.setBackground(mContext.getResources().getDrawable(R.drawable.shape_label_select));
//            holder.tv_label.setTextColor(Color.parseColor("#BF0B22"));
        } else {
//            holder.content.setBackground(mContext.getResources().getDrawable(R.drawable.shape_label_unselect));
//            holder.tv_label.setTextColor(Color.parseColor("#303030"));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        Glide.with(mContext).load(AppConfig.IMGURL + dataBean.getLogo()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);
        holder.restaurantName.setText(dataBean.getRestaurantName());
        if (dataBean.getShipping().equals("0")) {
            holder.shipping.setText("商家配送   " + "配送费" + dataBean.getShippingFee() + "元");
        }
        Date now = new Date();
// 把Date按照格式转换成字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(now));
// 把字符串按照格式转换成Date对象
        try {
            Date date1 = sdf.parse(dataBean.getOrderDate());
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日") ;

            String sdf2=format.format(date1);
//            String date = sdf.format(date1);//格式化数据bai

            holder.orderDate.setText(sdf2);
//            holder.orderDate.setText(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        DecimalFormat format = new DecimalFormat("");
        String str= format.format(dataBean.getScore());
        holder.score.setText(str);
    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_label;
        LinearLayout content;

        ImageView ivAvatar;
        TextView restaurantName;
        TextView shipping;
        TextView orderDate;
        TextView score;

        public ViewHolder(View itemView) {
            super(itemView);

            tv_label = (TextView) itemView.findViewById(R.id.tv_label);
            content = itemView.findViewById(R.id.content);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            restaurantName = itemView.findViewById(R.id.restaurantName);
            shipping = itemView.findViewById(R.id.shipping);
            orderDate = itemView.findViewById(R.id.orderDate);
            score = itemView.findViewById(R.id.score);
        }
    }


}
